package com.situ.music.activity;


import android.Manifest;
import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.ListView;

import com.situ.music.R;
import com.situ.music.adapter.FileAdapter;
import com.situ.music.util.PermissionUtils;

import java.io.File;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    // logt
    private static final String TAG = "MainActivity";

    // 代表这个应用不需要的所有权限
    private String[] requestPermissions = new String[]{Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE};

    // Java里面的File类，既可以表示文件也可以表示文件夹
    // 记录我当前所在文件夹,初始是sd卡根目录
    private File currentDirectory;
    private FileAdapter fileAdapter;
    private ListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView = (ListView) findViewById(R.id.listView);

        // 当前这个应用还没有的权限
        List<String> askPermissions = PermissionUtils.checkMorePermissions(this, requestPermissions);
        if (askPermissions.size() > 0) {// 有需要申请的权限
            PermissionUtils.requestMorePermissions(this, askPermissions, 1);
        } else {
            // 1、低版本手机，不需要申请权限
            // 2、高版本手机，用户之前已经授权的全部权限

            // 用户授权了  Alt+Enter
            currentDirectory = getSDCard();
            // 列出这个文件夹下面所有的文件和文件夹
           /* File[] files = currentDirectory.listFiles();
            for (File file : files) {
                // 得到文件的完整路径
                // System.out.println(file.getAbsolutePath());
                Log.d(TAG, file.getAbsolutePath());
            }*/

            fileAdapter = new FileAdapter(this);
            fileAdapter.setData(currentDirectory);
            listView.setAdapter(fileAdapter);
        }
    }

    // 得到用户授权的结果
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (PermissionUtils.isPermissionRequestSuccess(grantResults)) {
            // 用户授权了  Alt+Enter
            currentDirectory = getSDCard();
            // 列出这个文件夹下面所有的文件和文件夹
           /* File[] files = currentDirectory.listFiles();
            for (File file : files) {
                // 得到文件的完整路径
                Log.d(TAG, file.getAbsolutePath());
            }*/
            fileAdapter = new FileAdapter(this);
            fileAdapter.setData(currentDirectory);
            listView.setAdapter(fileAdapter);

        } else {
            // 销毁这个activity
            finish();
        }
    }

    /**
     * 获取SD根目录
     * @return
     */
    private File getSDCard() {
        File sdCard = null;
        boolean isSDCardExist = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
        if (isSDCardExist) {
            sdCard = Environment.getExternalStorageDirectory();
        } else {
            sdCard = Environment.getRootDirectory();
        }
        return  sdCard;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            if (!currentDirectory.equals(Environment.getExternalStorageDirectory())) {
                // 不是sd卡根目录
                String parentPath = currentDirectory.getParent();
                // 当按back键之后将上一级目录更新成最新的目录
                currentDirectory = new File(parentPath);
                fileAdapter.setData(currentDirectory);
            } else {
                // 已经是sd卡根目录
                finish();
            }

            // 代表程序自己处理了back键
            return  true;
        }

        return super.onKeyDown(keyCode, event);
    }

    /**
     * 更新最新目录
     * @param currentDirectory
     */
    public void updateCurrentDirectory(File currentDirectory) {
        this.currentDirectory = currentDirectory;
    }
}